package com.ys.util.shiro;

import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;

import com.ys.entity.shiro.LoginUser;

/**
 * shiro 工具类
 */
public class ShiroUtils{
    public static Subject getSubject(){
        return SecurityUtils.getSubject();
    }

    public static Session getSession(){
        return SecurityUtils.getSubject().getSession();
    }

    public static void logout(){
        getSubject().logout();
    }

    /**
     * 获取需要身份验证的功能
     * @return SysSetting 系统设置实体
     */
    public static Map<String, String> getAuthenticationModuleMap(){
    	@SuppressWarnings("unchecked")
		Map<String, String> authenticationModuleMap = (Map<String, String>) getSubject().getSession().getAttribute("authenticationModuleMap");
        return authenticationModuleMap;
    }

    public static void setSysUser(LoginUser user){
        Subject subject = getSubject();
        PrincipalCollection principalCollection = subject.getPrincipals();
        String realmName = principalCollection.getRealmNames().iterator().next();
        PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
        // 重新加载Principal
        subject.runAs(newPrincipalCollection);
    }
    
    /**
     * 获取当前登录用户信息
     */
    public static LoginUser getLoginUser(){
        LoginUser user = null;
        Object obj = getSubject().getPrincipal();
        if (obj != null){
            user = new LoginUser();
            BeanUtils.copyBeanProp(user, obj);
        }
        return user;
    }

    /**
     * 获取当前登录用户编号
     */
    public static Long getAdminId(){
    	LoginUser loginUser = getLoginUser();
    	if (loginUser == null) {
			return null;
		} else {
			return loginUser.getAdminId();
		}
    }

    /**
     * 获取当前登录用户名称
     */
    public static String getAdminName(){
        return getLoginUser().getAdminName();
    }

    /**
     * 获取当前登录用户角色ID
     */
    public static Long getRoleId(){
        return getLoginUser().getRoleId();
    }

    /**
     * 获取当前登录用户主角色名称
     */
    public static String getRoleName(){
        return getLoginUser().getRoleName();
    }
    
    /**
     * 获取登录IP
     */
    public static String getIp(){
        return getSubject().getSession().getHost();
    }

    /**
     * 获取SessionId
     */
    public static String getSessionId(){
        return String.valueOf(getSubject().getSession().getId());
    }

    public static void clearCachedAuthorizationInfo(){
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        UserRealm realm = (UserRealm) rsm.getRealms().iterator().next();
        realm.clearCachedAuthorizationInfo();
    }
    
    /**
     * 生成随机盐
     */
    public static String randomSalt()
    {
        // 一个Byte占两个字节，此处生成的3字节，字符串长度为6
        SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
        String hex = secureRandom.nextBytes(3).toHex();
        System.out.println("hex---" + hex);
        return hex;
    }
}
